<?php
/**
 * Created by PhpStorm.
 * User: zhanghua
 * Date: 2018/9/25
 * Time: 20:38
 */

namespace app\dlc\service;



use app\common\service\ModelBaseService;
use app\dlc\model\AdminRole;
use app\dlc\model\AdminRolePermRelation;
use app\dlc\model\AdminRoleRelation;
use think\Db;

class AdminRoleService extends ModelBaseService
{
    protected $modelName = 'app\\dlc\\model\\AdminRole';

    protected function getRoleByAdminId($adminId)
    {
        $list = AdminRoleRelation::where('admin_id', $adminId)->all();

        return $list;
    }


    /**
     * 删除角色
     * @param $roleId
     * @return bool
     * @throws \Exception
     */
    protected function adminRoleDel($roleId)
    {
        $roleId = max(0, intval($roleId));
        Db::startTrans();
        try {
            AdminRole::destroy($roleId);
            AdminRolePermRelation::destroy(function ($query) use ($roleId){
                $query->where('role_id', $roleId);
            });
            Db::commit();
            return true;
        } catch (\Exception $e) {
            Db::rollback();
            throw new \Exception("删除不成功!");
        }
    }



}